package com.kh.pajx.sop.view.action.order;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.SysArea;
import com.kh.pajx.sop.domain.order.ReplaceCardOrder;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.kh.pajx.sop.util.common.FormatDate;
import com.kh.pajx.sop.util.common.ValidatorUtils;
import com.kh.pajx.sop.util.sop.PropertyValidate;
import com.opensymphony.xwork2.ActionContext;

/**
 * 补卡单据Struts2控制器
 * 
 * @author: azzcsimp
 * @Createdate: 2014年11月12日 上午9:18:05
 */
@Controller
@Scope("prototype")
public class ReplaceCardOrderAction extends ModelDrivenBaseAction<ReplaceCardOrder> {
	
	private static final long serialVersionUID = 4879577265805298205L;
	
	// ==========================待审列表模块   begin=========================================
	
	/** 待审列表 */
	public String waits() {
		// 待审列表查询逻辑：
		// 查询单据状态为正常(1)，且进度标识为已提交(2)的单据, 并且按单据创建时间排序
		try {
			replaceCardOrderService.waits(model, pageNum, numPerPage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "waits";
	}
	
	/** 审核单据通过 */
	public void pass() {
		// == 审核通过处理逻辑：
		// == 1.获取原单据扩展信息状态设置为无效(0)
		// == 2.新增单据扩展信息，其中进度标识更改为审核通过(4)，并记录当前操作人员、时间，设置备注用于展示
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if (StringUtils.isBlank(model.getRcoId()) || !PropertyValidate.validateUUID(model.getRcoId())) {
				throw new Exception("单据编号非法");
			}
			replaceCardOrderRelationService.pass(model.getRcoId(), getCurrentUser().getUserId(), getCurrentUser().getUserName());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功通过该补卡单据！");
			dwz.setNavTabId("order_waits");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setStatusCode("300");
			dwz.setMessage("操作失败！错误信息：" + e.getMessage());
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 驳回原因页面 */
	public String turnUI() {
		return "turnUI";
	}

	/** 审核单据驳回 */
	public void turn() {
		// == 单据驳回处理逻辑：
		// == 1.获取原单据扩展信息状态设置为无效(0)
		// == 2.新增单据扩展信息，其中进度标识更改为审核驳回(0)，并记录当前操作人员、时间，存储驳回原因用于反馈客户查阅
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if (StringUtils.isBlank(model.getRcoId()) || !PropertyValidate.validateUUID(model.getRcoId())) {
				throw new Exception("单据编号非法");
			}
			replaceCardOrderRelationService.turn(model.getRcoId(), getCurrentUser().getUserId(), getCurrentUser()
					.getUserName(), model.getRemark());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功驳回该补卡单据！");
			dwz.setCloseDialog("order_turnUI");
			dwz.setNavTabId("order_waits");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setStatusCode("300");
			dwz.setMessage("操作失败！错误信息：" + e.getMessage());
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	// ==========================待审列表模块   end=========================================
	
	
	// ==========================已审列表模块   begin=========================================
	
	/** 已审列表 */
	public String already() {
		// 已审列表查询逻辑：
		// 查询单据状态为正常(1)，且进度标识为(>2 or =0)的单据, 并且按单据创建时间排序
		try {
			replaceCardOrderService.already(model, pageNum, numPerPage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "already";
	}
	
	/** 补卡完成 */
	public void close() {
		// == 补卡完成处理逻辑：
		// == 1.获取原单据扩展信息状态设置为无效(0)
		// == 2.新增单据扩展信息，其中进度标识更改为补卡完成(9)，并记录当前操作人员、时间，设置备注用于展示
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			ReplaceCardOrder rpCard = replaceCardOrderService.getById(model.getRcoId());
			if (rpCard.getProgStatus() == 9) {
				throw new Exception("该单据已被关闭，无需重复操作！");
			}
			if(rpCard.getProgStatus() == 0){
				throw new Exception("该单据已被驳回，无需设置完成！");
			}
			
			replaceCardOrderRelationService.close(model.getRcoId(), getCurrentUser().getUserId(), getCurrentUser().getUserName());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功关闭改补卡单据！");
			dwz.setNavTabId("order_already");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setStatusCode("300");
			dwz.setMessage("操作失败！错误信息：" + e.getMessage());
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 发货页面 */
	public String sendUI(){
		try {
			ReplaceCardOrder rpCard = replaceCardOrderService.getById(model.getRcoId());
			if(rpCard == null){
				throw new Exception("无法获取单据信息！");
			}
			if(rpCard.getProgStatus() == 5){
				throw new Exception("该单据已发货完毕，请勿重复操作！");
			}else if(rpCard.getProgStatus() == 9){
				throw new Exception("该单据已关闭，无需使用该功能！");
			}else if(rpCard.getProgStatus() == 0){
				throw new Exception("该单据已驳回，不能使用该功能！");
			}
			
		} catch (Exception e) {
			ActionContext.getContext().put("errorMsg", e.getMessage());
			e.printStackTrace();
		}
		
		return "addlogisticsUI";
	} 
	
	/** 发货 */
	public void send(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			ReplaceCardOrder rpCard = replaceCardOrderService.getById(model.getRcoId());
			if(rpCard == null){
				throw new Exception("无法获取单据信息！");
			}
			if(rpCard.getProgStatus() == 5){
				throw new Exception("该单据已发货完毕，请勿重复操作！");
			}else if(rpCard.getProgStatus() == 9){
				throw new Exception("该单据已关闭，无需使用该功能！");
			}else if(rpCard.getProgStatus() == 0){
				throw new Exception("该单据已驳回，不能使用该功能！");
			}
			replaceCardOrderRelationService.send(model.getRcoId(), getCurrentUser().getUserId(), getCurrentUser()
						.getUserName(), model.getLogisticsNo(), model.getLogisticsType());
			dwz.setStatusCode("200");
			dwz.setMessage("已成功发货！");
			dwz.setNavTabId("order_already");
			dwz.setCloseDialog("add_logistics");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			dwz.setStatusCode("300");
			dwz.setMessage("操作失败！错误信息：" + e.getMessage());
			e.printStackTrace();
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	// ==========================已审列表模块   end=========================================
	
	
	// ==========================确认收款模块   begin=========================================
	
	/** 确认收款查询页面 */
	public String changeList() {
		try {
			if (ValidatorUtils.validateFixedLengthNumber(model.getRcoNo(), 19, false)) {
				replaceCardOrderService.changeList(model, getPageNum(), getNumPerPage());
			} else {

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "changeList";
	}
	
	/** 确认收款数据收集页面 */
	public String changeUI() {
		return "changeUI";
	}

	/** 确认收款 */
	public void change() {
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if (getCurrentUser().getUserId().equals("8ae076ab4356b28f014356cb41270009")
					|| getCurrentUser().getUserId().equals("8ae076aa430ecbdf01430f7213e33cf8")) {
				String buyerEmail = ServletActionContext.getRequest().getParameter("rbuyerEmail");
				String tradeNo = ServletActionContext.getRequest().getParameter("rtradeNo");
				replaceCardOrderService.change(model.getRcoId(), getCurrentUser().getUserId(), getCurrentUser()
						.getUserName(), buyerEmail, tradeNo);
				dwz.setStatusCode("200");
				dwz.setMessage("已成功确认收款，该单据已交等待审核！");
				dwz.setNavTabId("order_changeList");
				dwz.setCloseDialog("order_changeUI");
				DwzAjaxResult.josnMessage(dwz);
			} else {
				dwz.setStatusCode("300");
				dwz.setMessage("您没有权限使用该功能");
				DwzAjaxResult.josnMessage(dwz);
			}

		} catch (Exception e) {
			dwz.setStatusCode("300");
			dwz.setMessage("操作失败！错误信息：" + e.getMessage());
			DwzAjaxResult.josnMessage(dwz);
			e.printStackTrace();
		}
	}
	
	// ==========================确认收款模块   end=========================================
	
	private HttpServletRequest request;
	private String areaCode;
	private String countyCode;
	private String startDate;
	private String endDate;

	

	

	/** 归档列表 */
	public String filing() {
		try {
			replaceCardOrderService.filing(model, pageNum, numPerPage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "filing";
	}

	public void filingOrder() {
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			ReplaceCardOrder rco = replaceCardOrderService.getById(model.getRcoId());
			rco.setRcoStatusFlag(3);
			rco.setDealOperId(getCurrentUser().getUserId());
			rco.setDealOperName(getCurrentUser().getUserName());
			rco.setDealTime(FormatDate.dateTimeDetail());
			replaceCardOrderService.update(rco);
			dwz.setStatusCode("200");
			dwz.setMessage("已成功归档单据(" + rco.getRcoNo() + ")！");
			dwz.setNavTabId("order_already");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setStatusCode("300");
			dwz.setMessage("操作失败！错误信息：" + e.getMessage());
			DwzAjaxResult.josnMessage(dwz);
		}
	}


	

	

	/**
	 *补卡统计 
	 */
	public String replaceCardCountUi() {
		request = ServletActionContext.getRequest();
		Map param = new HashMap<String, String>();
		param.put("sclName", model.getSclName());
		param.put("areaCode", areaCode);
		param.put("countyCode", countyCode);
		param.put("startDate", startDate);
		param.put("endDate", endDate);
		List<ReplaceCardOrder> orderList = replaceCardOrderService.getOrderList(param);
		List<SysArea> countyList = sysAreaService.findAreaByCity(areaCode);
		ActionContext.getContext().put("orderList", orderList);
		ActionContext.getContext().put("countyList", countyList);
		//补卡总数
		int cardCount = Integer.parseInt(replaceCardOrderService.getCardCount(param, "1").get(0).get(0));
		//补卡总费用
		int amount = Integer.parseInt(replaceCardOrderService.getCardCount(param, "2").get(0).get(0));
		//补卡单据数
		int orderCount = Integer.parseInt(replaceCardOrderService.getCardCount(param, "3").get(0).get(0));
		//收费的单据数
		int payOrderCount = Integer.parseInt(replaceCardOrderService.getCardCount(param, "4").get(0).get(0));
		//免费的单据数
		int freeOrderCount = Integer.parseInt(replaceCardOrderService.getCardCount(param, "5").get(0).get(0));
		ActionContext.getContext().put("cardCount", cardCount);
		ActionContext.getContext().put("amount", amount);
		ActionContext.getContext().put("orderCount", orderCount);
		ActionContext.getContext().put("payOrderCount", payOrderCount);
		ActionContext.getContext().put("freeOrderCount", freeOrderCount);
		return "replaceCardCountUi";
	}

	//---------------------------getter setter方法------------------------

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getCountyCode() {
		return countyCode;
	}

	public void setCountyCode(String countyCode) {
		this.countyCode = countyCode;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
}
